package com.yzy.controller.basic;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yzy.domain.entity.annotation.PassToken;
import com.yzy.domain.dto.system.AccountPermissionDTO;
import com.yzy.domain.entity.system.Account;
import com.yzy.domain.entity.system.AccountPermission;
import com.yzy.domain.entity.system.PermissionEnum;
import com.yzy.mapper.system.AccountMapper;
import com.yzy.mapper.system.AccountPermissionMapper;
import com.yzy.util.response.ResponseUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

/**
 * @author wyx
 * 2023年5月17日17:17:33
 */
@Api(tags = {"用户权限相关接口"})
@RequestMapping("/accountPermission")
@RestController
public class AccountPermissionController {

    @Resource
    private AccountPermissionMapper accountPermissionMapper;

    @Resource
    private AccountMapper accountMapper;

    @PassToken
    @ApiOperation(value = "权限列表")
    @GetMapping("/permissionList")
    public ResponseEntity<?> permissionList() {
        List<String> result = new ArrayList<>();
        for (PermissionEnum permissionEnum : PermissionEnum.values()) {
            result.add(permissionEnum.getCode());
        }
        return ResponseUtils.OK(result);
    }

    @PassToken
    @ApiOperation(value = "增【权限】")
    @PostMapping("/create")
    public ResponseEntity<?> create(@RequestBody AccountPermissionDTO dto) {
        Account account = dto.getAccount();
        accountMapper.insert(account);

        String accountId = account.getId();
        List<PermissionEnum> list = dto.getList();

        boolean flag = true;
        for (PermissionEnum permissionEnum : list) {
            AccountPermission entity = new AccountPermission(UUID.randomUUID().toString().replace("-", ""), permissionEnum, accountId);
            int insert = accountPermissionMapper.insert(entity);
            if (insert != 1) {
                flag = false;
            }
        }
        return ResponseUtils.OK(flag ? dto : "权限新增失败");

    }

    @PassToken
    @ApiOperation(value = "改【权限】")
    @PutMapping("/update")
    public ResponseEntity<?> update(@RequestBody AccountPermissionDTO dto) {
        String accountId = dto.getAccountId();
        List<PermissionEnum> list = dto.getList();

        LambdaQueryWrapper<AccountPermission> deleteWrapper = new LambdaQueryWrapper<>();
        deleteWrapper.eq(AccountPermission::getAccountId, accountId);
        accountPermissionMapper.delete(deleteWrapper);

        boolean flag = true;
        for (PermissionEnum permissionEnum : list) {
            AccountPermission entity = new AccountPermission(UUID.randomUUID().toString().replace("-", ""), permissionEnum, accountId);
            int insert = accountPermissionMapper.insert(entity);
            if (insert != 1) {
                flag = false;
            }
        }
        return ResponseUtils.OK(flag ? dto : "权限修改失败");
    }

    @PassToken
    @ApiOperation(value = "查【权限】-按用户id")
    @GetMapping("/retrievePermissionByAccountId")
    public ResponseEntity<?> retrieveProjectById(@RequestParam String accountId) {
        LambdaQueryWrapper<AccountPermission> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AccountPermission::getAccountId, accountId);
        wrapper.orderByAsc(AccountPermission::getPermission);
        List<AccountPermission> selectList = accountPermissionMapper.selectList(wrapper);

        List<String> result = selectList.stream().map(AccountPermission::getPermission).map(PermissionEnum::getCode).collect(Collectors.toList());
        return ResponseUtils.OK(result);
    }

}
